package cn.compose.online.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

@Component
public class ComplieStrategyHandlerFactory implements InitializingBean, ApplicationContextAware {

    private static final HashMap<String, IComplieStrategy> COMPLIE_HANDLER_MAP = new HashMap<>();
    private ApplicationContext appContext;


    public IComplieStrategy getHandler(String type) {
        return COMPLIE_HANDLER_MAP.get(type);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        appContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        appContext.getBeansOfType(IComplieStrategy.class)
                .values()
                .forEach(handler -> COMPLIE_HANDLER_MAP.put(handler.getType(), handler));
    }
}
